<?php


namespace app\admin\controller;


use easyadmin\app\columns\form\FormText;
use easyadmin\app\columns\form\FormTextarea;
use easyadmin\app\columns\lists\ListDateTime;
use easyadmin\app\columns\lists\ListText;
use easyadmin\app\libs\ListField;
use easyadmin\app\libs\Page;
use easyadmin\app\libs\PageForm;
use easyadmin\app\libs\Pagination;
use think\db\Query;

class News extends Admin
{
    protected $tableName = 'app_news';
    protected $softDeleteField = 'is_delete';

    protected function configListJoin(Page $page, Query $query, string $alias)
    {
        $query->join('manage', 'manage.id='.$alias.'.create_user_id');
    }

    protected function configListField(ListField $list)
    {
        $list
            ->addField('id', '新闻ID', ListText::class)
            ->addField('title', '新闻标题', ListText::class)
            ->addField('outline', '新闻简介', ListText::class)
            ->addField('manage.username', '创建人', ListText::class)
            ->addField('create_time', '创建时间', ListDateTime::class)
            ;
    }

    protected function configFormField(PageForm $page)
    {
            $page
                ->addField('title', '新闻标题', FormText::class,[
                    'required' => true
                ])
                ->addField('outline', '新闻简介', FormText::class, [
                    'required' => true
                ])
                ->addField('content', '新闻内容', FormTextarea::class, [
                    'required' => true
                ])
            ;
    }
    protected function configShowField(ListField $field)
    {
        $field
            ->addField('title', '新闻标题', ListText::class)
            ->addField('outline', '新闻简介', ListText::class)
            ->addField('content', '新闻内容', ListText::class)
            ;
    }

    protected function configListPagination(Pagination $pagination)
    {
        $pagination->setPageSize(20);
    }

    protected function insertBefore($data): array
    {
        $data['create_time'] = time();
        $data['create_user_id'] = $this->getLoginUserId();
        return $data;
    }
}